package top.hihuzi.controller;


import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.LongStream;

@Slf4j
@RestController
public class ConsumerController {
    @Autowired
    private LoadBalancerClient loadBalancerClient;
    @Autowired
    private RestTemplate restTemplate;

    @RequestMapping("/test")
    public String helloWorld(String s) {
        System.out.println("传入的值为：" + s);
        //第一种调用方式
        String forObject = restTemplate.getForObject("http://EURKA-CONSUMER", String.class);
        log.debug(forObject);
        //第二种调用方式
        //根据服务名 获取服务列表 根据算法选取某个服务 并访问某个服务的网络位置。
        ServiceInstance serviceInstance = null;
        loadBalancerClient.choose("EURKA-CONSUMER");
        String forObject1 = null;
        if (null != serviceInstance)
            forObject1 = new RestTemplate().getForObject("http://" + serviceInstance.getHost() + ":" + serviceInstance.getPort(), String.class);
        log.debug(forObject1);

        return forObject;
    }

    @RequestMapping("/person")
    public Object tosay1() {
        Object forObject = null;
        ServiceInstance choose=null;
        try {
            forObject = restTemplate.getForObject("http://ribbon-provide/provide", Object.class);
            System.out.println(forObject);
            forObject = restTemplate.getForObject("http://RIBBON-PROVIDE/provide", Object.class);
            System.out.println(forObject);
             choose = loadBalancerClient.choose("ribbon-provide");
             log.debug(choose!=null?choose.getHost():null);
             choose = loadBalancerClient.choose("RIBBON-PROVIDE");
            log.debug(choose!=null?choose.getHost():null);

        } catch (RestClientException e) {
            e.printStackTrace();
        } finally {
        }
        return forObject;
    }

    @RequestMapping("/")
    public List<String> tosay() {
        double asDouble = IntStream.of(1, 3, 1).average().getAsDouble();
        System.out.println(asDouble);
        return LongStream.range(1, 100).mapToObj(operand -> "".join("V", "--", "--")).collect(Collectors.toList());
    }
}
